CAN Gateway code example  v1.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
main.cpp
Go to the documentation of this file.
1 /***********************************************************************************************//**
2  * @file main.cpp
3  * @brief The main file of an example code for CAN Gateway with hdj2534.so (v1.0.226)
4  * @date 2014-03-06
5  * @copyright Hatteland Display AS
6  * @mainpage Hatteland Display CAN Gateway example for Linux
7  **************************************************************************************************/
8 
9 //==================================================================================================
10 // Includes
11 //==================================================================================================
12 #include <iostream>
13 #include <cstring>
14 #include <unistd.h>
15 #include <pthread.h>
16 #include <hdj2534.h>
17 #include "cangw.hpp"
18 
19 //==================================================================================================
20 // Namespaces
21 //==================================================================================================
22 
23 
24 //==================================================================================================
25 // Defines
26 //==================================================================================================
27 
28 
29 //==================================================================================================
30 // Typedefs
31 //==================================================================================================
32 
33 
34 //==================================================================================================
35 // Declarations of local functions
36 //==================================================================================================
37 static void rcvChannel1(const J2534::PASSTHRU_MSG * rxPassThruMsg, unsigned long rxMsgNum);
38 static void rcvChannel2(const J2534::PASSTHRU_MSG * rxPassThruMsg, unsigned long rxMsgNum);
39 
40 //==================================================================================================
41 // Variables
42 //==================================================================================================
43 /// Number of messages to send
44 static const unsigned int MESSAGES_TO_SEND = 50;
45 /// Period for periodic messages
46 static const unsigned int PERIODIC_MSG_INTERVAL = 50;
47 /// Marking for messages sent over channel 1
48 static const char CHANNEL_1_MARK = '1';
49 /// Marking for messages sent over channel 2
50 static const char CHANNEL_2_MARK = '2';
51 /// Marking for periodic messages
52 static const char PERIODIC_MSG_MARK = 'P';
53 /// Mutex used by callback functions during accessing the screen
54 static pthread_mutex_t screenMutex = PTHREAD_MUTEX_INITIALIZER;
55 
56 //==================================================================================================
57 //==================================================================================================
58 
59 /***********************************************************************************************//**
60  * @brief The main function
61  **************************************************************************************************/
62 int main()
63 {
64  using namespace std;
65 
67  &rcvChannel2))
68  {
69  J2534::PASSTHRU_MSG txPassThruMsg;
70  J2534::J2534_TxFlags txFlags;
71  unsigned int id;
72 
73  memset(&txPassThruMsg, 0, sizeof(J2534::PASSTHRU_MSG));
74  txFlags.bits.Can29BitId = 1;
75  txPassThruMsg.TxFlags = txFlags.value;
76  txPassThruMsg.ProtocolID = J2534::CAN;
77  id = 0x0000ABCD;
78  txPassThruMsg.Data[0] = static_cast<unsigned char>(id >> 24);
79  txPassThruMsg.Data[1] = static_cast<unsigned char>(id >> 16);
80  txPassThruMsg.Data[2] = static_cast<unsigned char>(id >> 8);
81  txPassThruMsg.Data[3] = static_cast<unsigned char>(id >> 0);
82  txPassThruMsg.Data[4] = CHANNEL_1_MARK; // source of a message - channel 1
83  txPassThruMsg.Data[5] = PERIODIC_MSG_MARK; // marking periodic message
84  txPassThruMsg.DataSize = 6;
87  {
88  cout << "CAN_GW periodic msg start ERROR! " << endl;
89  }
90  id = 0x0000BCDE;
91  txPassThruMsg.Data[0] = static_cast<unsigned char>(id >> 24);
92  txPassThruMsg.Data[1] = static_cast<unsigned char>(id >> 16);
93  txPassThruMsg.Data[2] = static_cast<unsigned char>(id >> 8);
94  txPassThruMsg.Data[3] = static_cast<unsigned char>(id >> 0);
95  txPassThruMsg.Data[4] = CHANNEL_2_MARK; // source of a message - channel 2
98  {
99  cout << "CAN_GW periodic msg start ERROR! " << endl;
100  }
101 
102  id = 0x00000055;
103  for (unsigned int i = 0; i < MESSAGES_TO_SEND; i++)
104  {
105  txPassThruMsg.Data[0] = static_cast<unsigned char>(id >> 24);
106  txPassThruMsg.Data[1] = static_cast<unsigned char>(id >> 16);
107  txPassThruMsg.Data[2] = static_cast<unsigned char>(id >> 8);
108  txPassThruMsg.Data[3] = static_cast<unsigned char>(id >> 0);
109  txPassThruMsg.Data[4] = CHANNEL_1_MARK; // source of a message - channel 1
110  txPassThruMsg.Data[5] = '0';
111  txPassThruMsg.Data[6] = '0';
112  txPassThruMsg.Data[7] = '0';
113  txPassThruMsg.Data[8] = '0';
114  txPassThruMsg.Data[9] = '0';
115  txPassThruMsg.Data[10] = (i / 10) + '0'; // a message number on each channel
116  txPassThruMsg.Data[11] = (i % 10) + '0'; // a message number on each channel
117  txPassThruMsg.DataSize = 12;
118  if (canGw::CAN_GW_OK != canGw::send(canGw::CHANNEL_1, &txPassThruMsg))
119  {
120  cout << "CAN_GW send ERROR! " << endl;
121  break;
122  }
123  txPassThruMsg.Data[4] = CHANNEL_2_MARK; // source of a message - channel 2
124  if (canGw::CAN_GW_OK != canGw::send(canGw::CHANNEL_2, &txPassThruMsg))
125  {
126  cout << "CAN_GW send ERROR! " << endl;
127  break;
128  }
129  // 5 ms delay for main loop
130  static const unsigned int LOOP_DELAY = 5 * 1000;
131  usleep(LOOP_DELAY);
132 
133  id++;
134  }
136  {
137  cout << "CAN_GW uninit ERROR!" << endl;
138  }
139  }
140  else
141  {
142  cout << "CAN_GW init ERROR!" << endl;
143  cout << "Has application got superuser privileges?" << endl;
144  cout << "Is CAN Gateway connected?" << endl;
145  }
146  return 0;
147 }
148 
149 //==================================================================================================
150 //==================================================================================================
151 
152 /***********************************************************************************************//**
153  * @brief Callback function for receive on channel 1
154  * @param[in] rxPassThruMsg Pointer to buffer with received messages
155  * @param[in] rxMsgNum Number of received messages
156  **************************************************************************************************/
157 static void rcvChannel1(const J2534::PASSTHRU_MSG * rxPassThruMsg, unsigned long rxMsgNum)
158 {
159  using namespace std;
160 
161  for (unsigned int i = 0; i < rxMsgNum; i++)
162  {
163  pthread_mutex_lock(&screenMutex);
164  cout << "CHANNEL_1_cb | msg_src:ch" << rxPassThruMsg[i].Data[4];
165  cout << " | id:" << static_cast<unsigned int>(rxPassThruMsg[i].Data[0]);
166  cout << " " << static_cast<unsigned int>(rxPassThruMsg[i].Data[1]);
167  cout << " " << static_cast<unsigned int>(rxPassThruMsg[i].Data[2]);
168  cout << " " << static_cast<unsigned int>(rxPassThruMsg[i].Data[3]);
169  if (PERIODIC_MSG_MARK == rxPassThruMsg[i].Data[5])
170  {
171  cout << " | periodic";
172  }
173  cout << endl;
174  pthread_mutex_unlock(&screenMutex);
175  }
176 }
177 /***********************************************************************************************//**
178  * @brief Callback function for receive on channel 2
179  * @param[in] rxPassThruMsg Pointer to buffer with received messages
180  * @param[in] rxMsgNum Number of received messages
181  **************************************************************************************************/
182 static void rcvChannel2(const J2534::PASSTHRU_MSG * rxPassThruMsg, unsigned long rxMsgNum)
183 {
184  using namespace std;
185 
186  for (unsigned int i = 0; i < rxMsgNum; i++)
187  {
188  pthread_mutex_lock(&screenMutex);
189  cout << "CHANNEL_2_cb | msg_src:ch" << rxPassThruMsg[i].Data[4];
190  cout << " | id:" << static_cast<unsigned int>(rxPassThruMsg[i].Data[0]);
191  cout << " " << static_cast<unsigned int>(rxPassThruMsg[i].Data[1]);
192  cout << " " << static_cast<unsigned int>(rxPassThruMsg[i].Data[2]);
193  cout << " " << static_cast<unsigned int>(rxPassThruMsg[i].Data[3]);
194  if (PERIODIC_MSG_MARK == rxPassThruMsg[i].Data[5])
195  {
196  cout << " | periodic";
197  }
198  cout << endl;
199  pthread_mutex_unlock(&screenMutex);
200  }
201 }
202 /**************************************************************************************************/
static pthread_mutex_t screenMutex
Mutex used by callback functions during accessing the screen.
Definition: main.cpp:54
Name of CAN Gateway's channel 2.
Definition: cangw.hpp:39
static const char PERIODIC_MSG_MARK
Marking for periodic messages.
Definition: main.cpp:52
ErrCode_e startPeriodic(Channel_e channel, J2534::PASSTHRU_MSG *txPassThruMsg, unsigned long interval)
Function starts CAN Gateway's periodic message.
Definition: cangw.cpp:263
static void rcvChannel1(const J2534::PASSTHRU_MSG *rxPassThruMsg, unsigned long rxMsgNum)
Callback function for receive on channel 1.
Definition: main.cpp:157
static const char CHANNEL_2_MARK
Marking for messages sent over channel 2.
Definition: main.cpp:50
Name of CAN Gateway's channel 1.
Definition: cangw.hpp:37
Header file for cangw.cpp.
ErrCode_e init(Baudrate_e baudChan1, Baudrate_e baudChan2, const RcvCb_t cbChan1, const RcvCb_t cbChan2)
Function initializes CAN Gateway device.
Definition: cangw.cpp:124
static const unsigned int MESSAGES_TO_SEND
Number of messages to send.
Definition: main.cpp:44
static void rcvChannel2(const J2534::PASSTHRU_MSG *rxPassThruMsg, unsigned long rxMsgNum)
Callback function for receive on channel 2.
Definition: main.cpp:182
int main()
The main function.
Definition: main.cpp:62
static const char CHANNEL_1_MARK
Marking for messages sent over channel 1.
Definition: main.cpp:48
static const unsigned int PERIODIC_MSG_INTERVAL
Period for periodic messages.
Definition: main.cpp:46
No error.
Definition: cangw.hpp:28
Baudrate 125 kbps.
Definition: cangw.hpp:46
ErrCode_e send(Channel_e channel, J2534::PASSTHRU_MSG *txPassThruMsg)
Function sends PASSTHRU frame via given channel.
Definition: cangw.cpp:228
ErrCode_e uninit()
Function uninitializes CAN Gateway device.
Definition: cangw.cpp:201